const { URL } = require("../../config/keys");
const express = require("express");
const router = express.Router();
//导入数据库的视图
const Type = require("../../models/Type");

router.post("/create", (req, res) => {
  //判断数据库中是否有email

  Type.findOne({
    name: req.body.name,
  }).then((user) => {
    if (user)
      res.json({
        success: false,
        msg: "该类型已经存在",
      });
    else {
      const type = new Type({
        name: req.body.name,
      });
      type
        .save()
        .then((user) =>
          res.json({
            success: true,
            msg: "保存成功",
          })
        )
        .catch((err) => console.log(err));
    }
  });
});

router.get("/getType", (req, res) => {
  Type.find().then((type) => {
    res.json({
      success: true,
      result: type,
    });
  });
});

router.get("/deleteType/:id", (req, res) => {
  Type.remove({ _id: req.params.id }).then((type) => {
    res.json({
      success: true,
      msg: "删除成功",
    });
  });
});

module.exports = router;
